package com.tanhua.recommend.listener;

import com.alibaba.fastjson.JSON;
import com.tanhua.domain.mongo.Publish;
import com.tanhua.domain.mongo.PublishScore;
import com.tanhua.dubbo.api.PublishApi;
import org.apache.dubbo.config.annotation.Reference;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.stereotype.Component;

import java.util.Map;

@Component
@RocketMQMessageListener(topic = "quanzi_publish", consumerGroup = "tanhua-quanzi-group")
public class PublishScoreListener implements RocketMQListener<String> {

    @Reference
    private PublishApi publishApi;

    @Autowired
    private MongoTemplate mongoTemplate;

//    @Autowired
//    private List<MovementHandler> handlerList;

    @Override
    // 1-发动态，2-浏览动态， 3-点赞， 4-喜欢， 5-评论，6-取消点赞，7-取消喜欢
    public void onMessage(String message) {
        // 1.将json转为map
        Map<String, Object> msg = JSON.parseObject(message, Map.class);
        Long userId = Long.valueOf(msg.get("userId").toString());
        Long pid = Long.valueOf(msg.get("pid").toString());
        String publishId = msg.get("publishId").toString();
        Integer type = Integer.valueOf(msg.get("type").toString());

        // 2.创建publishScore对象
        PublishScore publishScore = new PublishScore();
        publishScore.setUserId(userId);
        publishScore.setPublishId(pid);
        publishScore.setDate(System.currentTimeMillis());

        // 遍历handler
//        for (MovementHandler movementHandler : handlerList) {
//            // 该由哪个处理器执行
//            if(movementHandler.shouldHandle(type)){
//                movementHandler.handle(publishScore, type);
//            }
//        }

        switch (type) {
            case 1:
                // 发布动态
                int score = 0;
                Publish publish = publishApi.findById(publishId);
                if (publish.getTextContent().length() < 50) {
                    score = 1;
                } else if (publish.getTextContent().length() < 100) {
                    score = 2;
                } else {
                    score = 3;
                }
                if (publish.getMedias() != null && publish.getMedias().size() > 0) {
                    score += publish.getMedias().size();
                }
                publishScore.setScore((double) score);
                break;

            case 2:
                // 浏览动态
                publishScore.setScore(1D);
                break;

            case 3:
                // 点赞
                publishScore.setScore(5D);
                break;

            case 4:
                // 喜欢
                publishScore.setScore(8D);
                break;

            case 5:
                // 评论
                publishScore.setScore(10D);
                break;

            case 6:
                // 取消点赞
                publishScore.setScore(-5D);
                break;

            case 7:
                // 取消喜欢
                publishScore.setScore(-8D);
                break;
        }
        // 保存评分数据
        mongoTemplate.save(publishScore);

    }

}
